package com.hnevc.mallserver3.controller;

import com.hnevc.mallserver3.common.ResponseResult;
import com.hnevc.mallserver3.entity.Advertisement;
import com.hnevc.mallserver3.service.AdvertisementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/advertisement")
@Api(tags = "广告信息接口")
public class AdvertisementController {

    @Autowired
    private AdvertisementService advertisementService;

    @PostMapping("/add")
    @ApiOperation(value = "addAdvertisement", notes = "添加广告信息")

    public ResponseResult addAdvertisement(@RequestBody Advertisement advertisement) {
        advertisementService.save(advertisement);
        return ResponseResult.success(advertisement);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getAdvertisementById", notes = "根据ID获取广告")

    public ResponseResult getAdvertisementById(@PathVariable Long id) {
        Advertisement advertisement = advertisementService.getById(id);
        return ResponseResult.success(advertisement);
    }

    @GetMapping("/list")
    @ApiOperation(value = "listAdvertisementes", notes = "获取所有广告信息")
    public ResponseResult listAdvertisementes() {
        List<Advertisement> advertisementes = advertisementService.list();
        return ResponseResult.success(advertisementes);
    }

    @PutMapping("/update")
    @ApiOperation(value = "updateAdvertisement", notes = "更新广告信息")

    public ResponseResult updateAdvertisement(@RequestBody Advertisement advertisement) {
        advertisementService.updateById(advertisement);
        return ResponseResult.success(null);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "deleteAdvertisement", notes = "删除广告信息根据ID")

    public ResponseResult deleteAdvertisement(@PathVariable Long id) {
        advertisementService.removeById(id);
        return ResponseResult.success(null);
    }
}